package com.spi.soconversion.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.base.definition.Charge;
import com.spi.soconversion.util.QueryConstants;
import com.spi.soconversion.util.QueryReader;
import com.spi.soconversion.util.ResourceProvider;

public class ChargeDAOImpl implements ChargeDAO {
	
	private static final String LH_SKU = "LH_SKU";
	
	private static final String RH_SKU = "RH_SKU";

	private static final String NAME = "NAME";

	private static final String GROUP1 = "GROUP1";

	private static final String SKU2 = "SKU";

	public ResourceProvider provider;	
	
	static private org.apache.commons.logging.Log log = 
	    org.apache.commons.logging.LogFactory.getLog(ChargeDAOImpl.class);
	
	public ChargeDAOImpl() {
		provider = ResourceProvider.getInstance();
	}

	/**
	 * Get the charge information like charge amount, category and name
	 * 
	 */
	public HashMap<String, Charge> getChargeInfo() 
	throws SOCException {
		PreparedStatement ordersStatement = null;
		ResultSet rsCharges = null;
		HashMap<String, Charge> result = new HashMap<String, Charge>();
		try {
			String chargeInfoQuery = QueryReader.getQuery(QueryConstants.GET_CHARGE_INFO_QUERY);
			ordersStatement = provider.getdbConnection().prepareStatement(chargeInfoQuery);
			rsCharges = ordersStatement.executeQuery();
			
			while (rsCharges.next()) {
				String sku = rsCharges.getString(SKU2);
				Charge chargeInfo = new Charge();
				chargeInfo.setGroup(rsCharges.getString(GROUP1));
				chargeInfo.setName(rsCharges.getString(NAME));
				chargeInfo.setSku(sku);
				result.put(sku, chargeInfo);
			}	
			
			try {
				if (rsCharges != null) {
					rsCharges.close();
				}
				
				if(ordersStatement!=null) {
					ordersStatement.close();
				}			
			}
			catch (SQLException sqle) {
				sqle.printStackTrace();
			}
		}
		catch(SQLException sqle) {
			throw new SOCException ("Error in getChargeInfo()", sqle);
		}
		

		return result;				
	}

	/**
	 * 
	 */
	public HashMap<String, String> getLhfSKU() throws SOCException {
		HashMap<String, String> result = new HashMap<String, String>();
		Statement ordersStatement = null;
		ResultSet rsSKU = null;
		
		String ordersQuery = QueryReader.getQuery(QueryConstants.IDENTIFY_LEFT_HANDED_FRIDGE_INDICATOR);
		try {
			ordersStatement = provider.getdbConnection().createStatement();
			rsSKU = ordersStatement.executeQuery(ordersQuery);
		
			while (rsSKU.next()) {
				result.put(rsSKU.getString(LH_SKU), rsSKU.getString(RH_SKU));			
			}
		} catch (SQLException sqle) {
			log.error("Error in getOrders()", sqle);
			sqle.printStackTrace();
			throw new SOCException("Error in retrieving orders - getLhfSKU" , sqle);
		}
		
		try {
			if (rsSKU != null) {
				rsSKU.close();
			}
			
			if(ordersStatement!=null) {
				ordersStatement.close();
			}			
		}
		catch (SQLException sqle) {
			sqle.printStackTrace();
		}

		return result;				
	}
}